/**
 * @param {Object} url请求地址
 * @param {Object} data 请求数据
 * @param {Object} contentType 请求内容类型 1=json  2=form
 * @param {Object} method请求方法
 */
function request(
	url,
	data,
	method,
	contentType
) {
	let header = {
		'content-type': contentType === 1 ? 'application/x-www-form-urlencoded' : 'application/json',
		"token": uni.getStorageSync("orderToken")
	}
	let baseUrl = "http://103.254.76.2:6898/";
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			data,
			method,
			header,
			success: (res) => {
				if (res.statusCode === 200) {
					// 请求成功
					resolve(res.data);
					if (res.data && res.data.code == 600) {
						uni.showToast({
							icon: 'none',
							title: "未登录或登录状态已超时",
							duration: 1500
						});
					}
				} else if (res.statusCode === 401) {
					uni.showToast({
						icon: 'none',
						title: "未登录或登录状态已超时",
						duration: 1500
					});
				} else if (res.statusCode === 405) {
					uni.showToast({
						icon: 'none',
						title: "请求方法错误",
						duration: 1500
					});
				} else {
					uni.showToast({
						icon: 'none',
						title: "请求错误" + res.statusCode,
						duration: 1500
					});
				}
			},
			fail: (err) => {
				console.log("err:", err)
				uni.showToast({
					icon: 'none',
					title: err.errMsg,
					duration: 1500
				});
				reject(err);
			}
		})
	})
}

export default {
	get: function(url, data, contentType) {
		return request(url, data, 'GET', contentType)
	},
	post: function(url, data, contentType) {
		return request(url, data, 'POST', contentType)
	},
	put: function(url, data, contentType) {
		return request(url, data, 'PUT', contentType)
	},
	delete: function(url, data, contentType) {
		return request(url, data, 'DELETE', contentType)
	}
}